"""
Author : Mr.Sun
DateTime : 2022/9/26.16:58
FileName : OperateSqlite.py
Desc :  使用sqlite 进行数据保存和去重
"""

import os
import sqlite3
from utils.logger import logger

project_dir = os.path.dirname(os.path.dirname(__file__))

connect = sqlite3.connect(project_dir + '/data.db')


# 获取cursor对象
class sqliteOperate(object):
    def __init__(self):
        self.cursor = connect.cursor()

    def insert_data(self, sql, data):
        try:
            # 单条插入
            self.cursor.execute(sql, data)
            connect.commit()
        except Exception as e:
            connect.rollback()
            logger.error("insert error is : " + str(e))

    def search_data(self, sql):
        try:
            self.cursor.execute(sql)
            # 获取所有数据
            data_all = self.cursor.fetchall()
            return data_all
        except Exception as e:
            logger.error("search data error is " + str(e))

    def search_data_email(self, sql, args=None):
        try:
            self.cursor.execute(sql, args)
            # 获取所有数据
            data_all = self.cursor.fetchall()
            return data_all
        except Exception as e:
            logger.error("search data error is " + str(e))
